import {layer} from "@layui/layui-vue";

import {StringUtils} from '@/fly/utils/StringUtils';
import useProductClazzInfoBuilder, {
    ProductClazzInfo,
    ProductClazzInfoQuery
} from '@/api/product/model/ProductClazzInfoModel';
import ProductClazzInfoApi from '@/api/product/ProductClazzInfoApi';
import {FormTypeEnum} from "@/fly/biz/BizModel";
import {LayTableUtils} from "@/fly/utils/LayTableUtils";
import useLayTableComposable from "@/fly/table";
import {ref} from "vue";

const {
    productClazzInfo,
    productClazzInfoColumns,
    queryProductClazzInfoParams,
    tableProductClazzInfoData,
    refProductClazzInfoComponent
} = useProductClazzInfoBuilder();
const {flyLayPage} = useLayTableComposable();

const productClazzInfoApi = new ProductClazzInfoApi();


/**
 * 产品类目 处理
 *
 * @author 王大宸
 * @date 2025-03-04 15:48:47
 */
export default function useProductClazzInfoComposable() {

    /**
     * 加载分页数据
     * @author 王大宸
     * @param params
     */
    const loadTableProductClazzInfoData = (params: ProductClazzInfoQuery) => {
        tableProductClazzInfoData.loading = true;
        productClazzInfoApi.page(params).then(res => {
            tableProductClazzInfoData.total = res.data.total;
            tableProductClazzInfoData.rows = res.data.rows;
            tableProductClazzInfoData.loading = false;
        });
    };

    /**
     * 分页页面搜索框form
     */
    const refResetQueryProductClazzInfo = ref();

    /**
     * 重置搜索表单
     */
    const resetQueryProductClazzInfo = () => {
        refResetQueryProductClazzInfo.value.reset();
        queryProductClazzInfoParams.parentId = '';
        queryProductClazzInfoParams.clazzTitle = '';
        queryProductClazzInfoParams.validFlag = '';
    }

    /**
     * 重新加载数据
     * @author 王大宸
     * @param page
     * @param limit
     */
    const reloadProductClazzInfoDate = (page: number = 1, limit: number = 10) => {
        queryProductClazzInfoParams.page = page;
        queryProductClazzInfoParams.limit = limit;
        loadTableProductClazzInfoData(queryProductClazzInfoParams);
    };

    /**
     * 新增按钮
     */
    const onTapProductClazzInfoAdd = () => {
        refProductClazzInfoComponent.value?.open(FormTypeEnum.ADD, null);
    }

    /**
     * 编辑按钮
     * @param data 编辑时的数据信息
     */
    const onTapProductClazzInfoEdit = (data: ProductClazzInfo) => {
        refProductClazzInfoComponent.value?.open(FormTypeEnum.EDIT, data);
    }

    /**
     * 查看按钮
     * @param data 查看时的数据信息
     */
    const onTapProductClazzInfoView = (data: ProductClazzInfo) => {
        refProductClazzInfoComponent.value?.open(FormTypeEnum.VIEW, data);
    }

    /**
     * 数据保存处理(新增/编辑)
     * @param data 保存的数据
     */
    const productClazzInfoDataHandler = (data: ProductClazzInfo) => {
        if (StringUtils.isEmpty(data.id)) {
            productClazzInfoApi.saveInfo(data).then((res) => {
                reloadProductClazzInfoDate(1, flyLayPage.limit); // 表格重新加载数据
                layer.msg(res.msg, {time: 1500, icon: 1}); // 操作成功提示
                refProductClazzInfoComponent.value?.onTapClose();  // 关闭弹窗
            })
        } else {
            productClazzInfoApi.updateInfo(data, data.id).then((res) => {
                reloadProductClazzInfoDate(1, flyLayPage.limit);  // 表格重新加载数据
                layer.msg(res.msg, {time: 1500, icon: 1}); // 操作成功提示
                refProductClazzInfoComponent.value?.onTapClose();  // 关闭弹窗
            })
        }
    }


    /**
     * 删除按钮
     * @param id 需要删除的数据主键
     */
    const removeProductClazzInfoHandler = (id: string, title?: string) => {
        LayTableUtils.remove({
            title: title,
            callback: () => {
                productClazzInfoApi.remove(id).then((res) => {
                    reloadProductClazzInfoDate(1, flyLayPage.limit);
                    layer.msg(res.msg, {time: 1500, icon: 1});
                })
            }
        })
    }


    return {
        refProductClazzInfoComponent,
        refResetQueryProductClazzInfo,
        resetQueryProductClazzInfo,
        productClazzInfo,
        tableProductClazzInfoData,
        productClazzInfoColumns,
        queryProductClazzInfoParams,
        /* 加载表格 */
        reloadProductClazzInfoDate,
        loadTableProductClazzInfoData,

        /* 新增, 编辑, 查看按钮 */
        onTapProductClazzInfoAdd,
        onTapProductClazzInfoEdit,
        onTapProductClazzInfoView,

        /* 接口 */
        removeProductClazzInfoHandler,
        productClazzInfoDataHandler,
        flyLayPage

    };
}